<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class branch extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('settings/branch_model');
		$this->load->library('datatables');		
	}
	public function get(){
		echo $this->branch_model->get();
	}
		
	
	function create_branch()
	{
		$this->data['page_title'] = "Create branch";
		$meta['page_title'] = "Create branch";
		
	//	$this->data['groups']=$this->accgroup_model->get_group();
		/*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}for checking the authentication*/

		//validate form input
		$this->form_validation->set_rules('name', 'branch Name', 'required|is_unique[branch.name]');
		//$this->form_validation->set_rules('code', 'Invoice Code', 'required|is_unique[branch.code]');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean|min_length[10]|max_length[10]');
		$this->form_validation->set_rules('address', 'Address', 'required');
		if ($this->form_validation->run() == true)
		{
			$additional_data = array(
				'name' =>strtoupper( $this->input->post('name')),
				'code' =>strtoupper( $this->input->post('code')),
				'phone_no' => $this->input->post('phone_no'),
				'address' => strtoupper($this->input->post('address')),
	
			);
		}
		if ($this->form_validation->run() == true) 
		{
			$this->branch_model->insert($additional_data);
			redirect("settings/branch/get_all_branch");
			//check to see if we are creating the user
			//redirect them back to the admin page
			//$this->session->set_flashdata('message', $this->ion_auth->messages());
			//redirect("auth", 'refresh');
		}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);

			$this->data['code'] = array(
				'name'  => 'code',
				'id'    => 'code',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('code'),
			);
	
			$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no'),
			);
	
			$this->data['address'] = array(
			'name'  => 'address',
			'id'    => 'address',
			'type'  => 'textarea',
			'value' => $this->form_validation->set_value('address'),
			);
		
			
		//	print_r($this->data);exit;

			$this->load->view('common/header',$meta);
			$this->load->view('settings/branch/create_branch', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function get_all_branch(){
		//list the users
		
	$this->data['users'] = $this->branch_model->get_branch()->result();
			
//			print_r($this->data['users']);exit; 
	// $this->data['user_type'] =  $this->ion_auth_model->get_user_type($this->session->userdata('user_id'))->row();
	  //echo $get_user_type->name;
	   
	  // exit;
	   $meta['page_title'] = "branch List";
	   $this->data['message'] = "branch Added Successfully";
	 //  $this->session->set_flashdata('message', 'User Created Successfully');
	   $this->load->view('common/header', $meta);
	   $this->load->view('settings/branch/view_branch1');
       $this->load->view('common/footer');
	   
		}
	

	function edit_branch($id)
	{
		
		$meta['page_title'] = "Edit branch";
		//$this->data['groups']=$this->branch_model->get_branch();
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}

		$user = $this->branch_model->edit_branch($id)->row();
		//print_r($user);exit;
		//$groups=$this->ion_auth->groups()->result_array();
		//$currentGroups = $this->ion_auth->get_users_groups($id)->result();
		
		//getting the user type ex. team lead 
		//$get_user_type = $this->ion_auth_model->user_type()->result_array();
		 //$currentUser = $this->ion_auth_model->get_user_type($id)->result();
		
		//getting the user type ex. fedonic 
		//$get_departments = $this->ion_auth_model->departments()->result_array();
		 //$currentDepartments = $this->ion_auth_model->get_user_department($id)->result();
		
		//process the phone number
		//if (isset($user->phone) && !empty($user->phone))
	//	{
			//$user->phone = explode('-', $user->phone);
		//}

		//validate form input
		$this->form_validation->set_rules('name', 'Branch Name', 'required|xss_clean');
		$this->form_validation->set_rules('code', 'Invoice Code', 'required|xss_clean');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean');
		$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
		/*$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
		$this->form_validation->set_rules('branch', 'branch Name', 'required|xss_clean');*/
		
		//$this->form_validation->set_rules('groups', 'Groups', 'xss_clean');

		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/

			$data = array(
				'name' => strtoupper($this->input->post('name')),
				'code' => strtoupper( $this->input->post('code')),
				'phone_no' => $this->input->post('phone_no'),
				'Address' => strtoupper($this->input->post('address'))
			);
			//print_r($data);
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if ($this->form_validation->run() === TRUE)
			{
				$this->branch_model->update($user->id, $data);
				redirect("settings/branch/get_all_branch");
				//check to see if we are creating the user
				//redirect them back to the admin page
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		//pass the user to the view
		$this->data['user'] = $user;
		//$this->data['groups'] = $groups;
		//$this->data['currentGroups'] = $currentGroups;
		
		//user type 
		
		//$this->data['user_type'] = $get_user_type;
	//	$this->data['currentUser'] = $currentUser;
		
		//user departments
		
		//$this->data['get_departments'] = $get_departments;
		//$this->data['currentDepartments'] = $currentDepartments;		
		

		$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
		);

		$this->data['code'] = array(
			'name'  => 'code',
			'id'    => 'code',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('code', $user->code),
		);




		$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no', $user->phone_no),
		);
		
		$this->data['address'] = array(
			'name'  => 'address',
			'id'    => 'address',
			'type'  => 'textarea',
			'rows'   =>5,
			'value' => $this->form_validation->set_value('address', $user->address),
		);
		
		/*$this->data['phone2'] = array(
			'name'  => 'phone2',
			'id'    => 'phone2',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
		);
		$this->data['phone3'] = array(
			'name'  => 'phone3',
			'id'    => 'phone3',
			'type'  => 'text',
			'class' => 'span2',
			'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
		);*/
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Update',
			'class' =>'btn btn-inverse'
		);
		
		//print_r($this->data);exit;
		$this->data['page_title'] = "Edit branch";
		 $this->load->view('common/header',$meta);
		$this->load->view('settings/branch/edit_branch', $this->data, $id);
		 $this->load->view('common/footer');
	}

	

	function delete_branch($id){
		$this->branch_model->delete($id);
		redirect("settings/branch/get_all_branch");		
	}


}
?>